#include <MapKit/MapKit.h>

void MapKit::_cell_t::Sort()
{
    if (A < B && B < C)
        ;
    else if (A < C && C < B)
    {
        coord_t t;
        t.first = B.first;
        t.second = B.second;
        B.first = C.first;
        B.second = C.second;
        C.first = t.first;
        C.second = t.second;
    }
    else if (B < A && A < C)
    {
        coord_t t;
        t.first = A.first;
        t.second = A.second;
        A.first = B.first;
        A.second = B.second;
        B.first = t.first;
        B.second = t.second;
    }
    else if (B < C && C < A)
    {
        coord_t t;
        t.first = A.first;
        t.second = A.second;
        A.first = B.first;
        A.second = B.second;
        B.first = C.first;
        B.second = C.second;
        C.first = t.first;
        C.second = t.second;
    }
    else if (C < A && A < B)
    {
        coord_t t;
        t.first = C.first;
        t.second = C.second;
        C.first = B.first;
        C.second = B.second;
        B.first = A.first;
        B.second = A.second;
        A.first = t.first;
        A.second = t.second;
    }
    else if (C < B && B < A)
    {
        coord_t t;
        t.first = A.first;
        t.second = A.second;
        A.first = C.first;
        A.second = C.second;
        C.first = t.first;
        C.second = t.second;
    }
}

bool MapKit::_cell_t::operator==(const _cell_t& c)
{
    return A == c.A && B == c.B && C == c.C;
}

MapKit::MapKit()
{
    coords = new vtvec_t;
    vertices = new mkpvtvec_t;
    triangles = new mkptrivec_t;
    altitude = new std::vector<std::vector<double>>;
}

MapKit::~MapKit()
{
    for (MKVertex* p : *vertices)
    {
        p->Destroy();
        p = nullptr;
    }
    for (MKTriangle* p : *triangles)
    {
        p->Destroy();
        p = nullptr;
    }
    delete coords;
    coords = nullptr;
    delete vertices;
    vertices = nullptr;
    delete triangles;
    triangles = nullptr;
    delete altitude;
    altitude = nullptr;
}

void MapKit::SetSize(const double& w, const double& h)
{
    width = w;
    height = h;
}

const MapKit::mkpvtvec_t* MapKit::GetVertices() const
{
    return vertices;
}

const MapKit::mkptrivec_t* MapKit::GetTriangles() const
{
    return triangles;
}

const int MapKit::GetFractalSize()
{
    return fractalSize;
}

const std::vector<std::vector<double>>* MapKit::GetAltitude() const
{
    return altitude;
}